package cn.stylefeng.guns.modular.storage.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.entity.BasicArea;
import cn.stylefeng.guns.modular.basic.entity.BasicCustomer;
import cn.stylefeng.guns.modular.basic.entity.BasicGoods;
import cn.stylefeng.guns.modular.basic.entity.BasicOperate;
import cn.stylefeng.guns.modular.basic.entity.BasicPosition;
import cn.stylefeng.guns.modular.basic.entity.BasicTray;
import cn.stylefeng.guns.modular.basic.entity.BasicWarehouse;
import cn.stylefeng.guns.modular.basic.model.params.BasicCustomerParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicGoodsParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicOperateParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicPositionParam;
import cn.stylefeng.guns.modular.basic.model.params.BasicTrayParam;
import cn.stylefeng.guns.modular.basic.model.result.BasicCustomerResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicGoodsResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicOperateResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicTrayResult;
import cn.stylefeng.guns.modular.basic.model.result.BasicWarehouseResult;
import cn.stylefeng.guns.modular.basic.service.BasicAreaService;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.basic.service.BasicGoodsService;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.modular.basic.service.BasicPositionService;
import cn.stylefeng.guns.modular.basic.service.BasicTrayService;
import cn.stylefeng.guns.modular.basic.service.BasicWarehouseService;
import cn.stylefeng.guns.modular.order.model.params.OutOrderParam;
import cn.stylefeng.guns.modular.storage.entity.Kfgoods;
import cn.stylefeng.guns.modular.storage.entity.Kfsum;
import cn.stylefeng.guns.modular.storage.entity.StorageStock;
import cn.stylefeng.guns.modular.storage.model.params.StorageStockManagementParam;
import cn.stylefeng.guns.modular.storage.model.params.StorageStockParam;
import cn.stylefeng.guns.modular.storage.model.result.StorageStockResult;
import cn.stylefeng.guns.modular.storage.service.StorageStockDetailedService;
import cn.stylefeng.guns.modular.storage.service.StorageStockManagementService;
import cn.stylefeng.guns.modular.storage.service.StorageStockService;
import cn.stylefeng.guns.sys.modular.system.entity.Dept;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.model.result.DictResult;
import cn.stylefeng.guns.sys.modular.system.service.DeptService;
import cn.stylefeng.guns.sys.modular.system.service.DictService;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;

/**
 * 库存表控制器
 */
@Controller
@RequestMapping("/storageStock")
public class StorageStockController extends BaseController {

    private String PREFIX = "/storage/storageStock";

    @Autowired
    private StorageStockService storageStockService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicOperateService basicOperateService;

    @Autowired
    private BasicCustomerService basicCustomerService;

    @Autowired
    private BasicWarehouseService basicWarehouseService;

    @Autowired
    private BasicAreaService basicAreaService;

    @Autowired
    private DictService dictService;

    @Autowired
    private BasicTrayService basicTrayService;

    @Autowired
    private BasicPositionService basicPositionService;

    @Autowired
    private BasicGoodsService basicGoodsService;

    /**
     * 跳转到库存监控按托页面
     */
    @RequestMapping("toStockList")
    public String toStockList(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	model.addAttribute("basicCustomerList", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	//查询可选择仓库
    	model.addAttribute("basicWarehouseList", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));

        // 商品大类COMMODITY_CATEGORY
//        List<DictResult> dictResultList3 = dictService.findByDicttypeCode2("COMMODITY_CATEGORY");
//        model.addAttribute("dictResultList3",dictResultList3);
        //商品类别
//        List<DictResult> dictResultList4 = dictService.findByDicttypeCodeAndDictCode3("COMMODITY_CATEGORY","GJ");
//        model.addAttribute("dictResultList4",dictResultList4);
        return PREFIX + "/stockMonitor.html";
    }

    /**
     * 查询库存监控按托列表
     */
    @ResponseBody
    @RequestMapping("/stockList")
    public LayuiPageInfo stockList(StorageStockParam storageStockParam,String startPutTime,String endPutTime,String startSingleTime,String endSinglePutTime) {
    	User user = userService.getcurrentUser();
    	storageStockParam.setDeptId(user.getDeptId());
    	storageStockParam.setDeliverId(user.getDeliverId());
        return this.storageStockService.findStockPageBySpec(storageStockParam,startPutTime,endPutTime,startSingleTime,endSinglePutTime,user.getOperateId());
    }

    /**
     * 导出Excel
     */
    @RequestMapping("/exportExcelBySpec")
    public void exportExcelBySpec(HttpServletResponse response, StorageStockParam storageStockParam,String startPutTime,String endPutTime,String startSingleTime,String endSinglePutTime) throws IOException {
    	if (ToolUtil.isNotEmpty(startPutTime) && ToolUtil.isNotEmpty(endPutTime)) {
    		storageStockParam.setPutTimeBetween("'"+startPutTime+"' and '"+endPutTime+"'");
		}
    	if (ToolUtil.isNotEmpty(startSingleTime) && ToolUtil.isNotEmpty(endSinglePutTime)) {
    		storageStockParam.setProducetimeBetween("'"+startSingleTime+"' and '"+endSinglePutTime+"'");
		}
        Workbook wb = storageStockService.exportToExcel(storageStockParam);
        OutputStream output = response.getOutputStream();
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String(("Inventory Monitoring by Pallet Information" + ".xlsx").getBytes(), "iso-8859-1"));
        wb.write(output);
        output.close();
    }

    /**
     * 跳转到库存监控按品页面
     */
    @RequestMapping("toStockListGoods")
    public String toStockListGoods(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	model.addAttribute("basicCustomerList", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	//查询可选择仓库
    	model.addAttribute("basicWarehouseList", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/stockMonitorGoods.html";
    }

    /**
     * 查询库存监控按品列表
     */
    @ResponseBody
    @RequestMapping("/stockListGoods")
    public LayuiPageInfo stockListGoods(StorageStockParam storageStockParam) {
    	User user = userService.getcurrentUser();
    	storageStockParam.setDeptId(user.getDeptId());
		storageStockParam.setDeliverId(user.getDeliverId());
        return this.storageStockService.findStockPageBySpecGoods(storageStockParam,user.getOperateId());
    }

    /**
     * 跳转到库存监控按效期页面
     */
    @RequestMapping("toStockListProducetime")
    public String toStockListProducetime(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	model.addAttribute("basicCustomerList", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	//查询可选择仓库
    	model.addAttribute("basicWarehouseList", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/stockMonitorProducetime.html";
    }

    /**
     * 查询库存监控按效期列表
     */
    @ResponseBody
    @RequestMapping("/stockListProducetime")
    public LayuiPageInfo stockListProducetime(StorageStockParam storageStockParam) {
    	User user = userService.getcurrentUser();
    	storageStockParam.setDeptId(user.getDeptId());
        return this.storageStockService.findStockPageBySpecProducetime(storageStockParam,user.getOperateId());
    }

    /**
     * 跳转到库存监控按库位页面
     */
    @RequestMapping("toStockListPositionNumber")
    public String toStockListPositionNumber(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	model.addAttribute("basicCustomerList", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	//查询可选择仓库
    	model.addAttribute("basicWarehouseList", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/stockListPositionNumber.html";
    }

    /**
     * 查询库存监控按库位列表
     */
    @ResponseBody
    @RequestMapping("/stockListPositionNumber")
    public LayuiPageInfo stockListPositionNumber(StorageStockParam storageStockParam) {
    	User user = userService.getcurrentUser();
    	storageStockParam.setDeptId(user.getDeptId());
        return this.storageStockService.findStockPageBySpecPositionNumber(storageStockParam,user.getOperateId());
    }

    /**
     * 跳转到 实时库存统计页面
     */
    @RequestMapping("shishikucun")
    public String shishikucun(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	model.addAttribute("basicCustomerList", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	//查询可选择仓库
    	model.addAttribute("basicWarehouseList", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/shishikucun.html";
    }

    /**
     * 查询 实时库存列表
     */
    @ResponseBody
    @RequestMapping("/findshishikucun")
    public LayuiPageInfo findshishikucun(StorageStockParam storageStockParam) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	storageStockParam.setDeptId(deptId);
    	storageStockParam.setDeliverId(deliverId);
    	if(storageStockParam.getWarehouseId() == null) {
    		List<BasicWarehouseResult> wlist = basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId());
    		if(wlist.size() > 0) {
    			storageStockParam.setWarehouseId(Long.parseLong(wlist.get(0).getId().toString()));
    		}
    	}

    	List<StorageStockResult> list = this.storageStockService.findshishikucun(storageStockParam,DateUtil.getDate(),user.getOperateId());
    	LayuiPageInfo layuiPageInfo = new LayuiPageInfo();
    	layuiPageInfo.setData(list);
    	layuiPageInfo.setCount(list.size());
        return layuiPageInfo;
    }

    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/stockMonitor.html";
    }

    /**
     * 导入页面
     * @param model
     * @return
     */
    @RequestMapping("/toimportExcel")
    public String toimportExcel(Model model) {
        prepareViewData(model);
        return PREFIX + "/storageStock_import.html";
    }

    /**
     * 选择商品页面
     * @param customerId
     * @param model
     * @return
     */
    @RequestMapping("/toChoiceGoods")
    public String toChoiceGoods() {
        return PREFIX + "/storageStockToChoiceGoods.html";
    }

    /**
     * 准备回显数据
     */
    private void prepareViewData(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	model.addAttribute("customerlist", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));

        //查询可选择分公司
        model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));

        //查询可选择仓库
        model.addAttribute("wlist", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
    }

    /**
     * 跳转到存货处置页面
     */
    @RequestMapping("cunhuochuzhi")
    public String cunhuochuzhi(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	model.addAttribute("basicCustomerList", basicCustomerService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
    	//查询可选择仓库
    	model.addAttribute("basicWarehouseList", basicWarehouseService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/stockMonitorGoodsChuzhi.html";
    }

    /**
     * 查询 基础字典
     * 三级数据
     */
    @RequestMapping("/queryGoodsType")
    @ResponseBody
    public List<DictResult> queryGoodsType(String code,String dictcode){
        List<DictResult> list = dictService.findByDicttypeCodeAndDictCode3(code,dictcode);
    	return list;
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add(Model model) {
        prepareViewData(model);
        return PREFIX + "/storageStock_add.html";
    }

    @Autowired
    private StorageStockManagementService storageStockManagementService;

    @Autowired
    private StorageStockDetailedService storageStockDetailedService;

    /**
     * 存货处置
     * 修改页面
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/chuzhi_edit.html";
    }

    /**
     * 存货处置
     * 修改
     */
    @RequestMapping("/chuzhiEdit")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public ResponseData chuzhiEdit(StorageStockParam storageStockParam,String nproducetime,String ngoodsbatchs,String nputTime,String cnum,String nodes) {
    	User user = userService.getcurrentUser();
    	StorageStock storageStock = storageStockService.getById(storageStockParam.getId());
    	BasicArea basicArea = basicAreaService.getById(storageStock.getAreaId());
    	BasicGoods basicGoods = basicGoodsService.getById(storageStock.getGoodsId());
    	if(!storageStockParam.getPutTime().equals(nputTime)|| !storageStockParam.getProducetime().equals(nproducetime) || !storageStockParam.getGoodsbatchs().equals(ngoodsbatchs)) {
    		BigDecimal as = new BigDecimal(0);
    		BigDecimal num = new BigDecimal(storageStock.getNum());//库存数
    		BigDecimal cnumbig = new BigDecimal(cnum);//处理数
    		if(cnumbig.compareTo(as) == -1 || cnumbig.compareTo(as) == 0) {
    	        return ResponseData.error("Cannot be less than or equal to 0");
    		}
    		if(cnumbig.compareTo(num) == 1) {
    	        return ResponseData.error("Cannot be greater than inventory quantity");
    		}

			StorageStockManagementParam management = new StorageStockManagementParam();//处置记录
			management.setPanType("Edit");
			management.setWarehouseId(storageStock.getWarehouseId());
			management.setWarehouseName(storageStock.getWarehouseName());
			management.setAreaId(storageStock.getAreaId());
			management.setAreaName(basicArea.getAreaName());
			management.setPositionId(storageStock.getPositionId());
			management.setPositionNumber(storageStock.getPositionNumber());
			management.setTrayId(storageStock.getTrayId());
			management.setTrayNumber(storageStock.getTrayNumber());
			management.setGoodsId(storageStock.getGoodsId());
			management.setGoodsName(storageStock.getGoodsName());
			management.setGoodsNumber(storageStock.getGoodsNumber());
			management.setGoodsUnit(basicGoods.getGoodsUnit());
			management.setGoodsSpecs(basicGoods.getGoodsSpecs());
			management.setNum(storageStock.getNum());
			management.setVolume(storageStock.getStockVolume());
			management.setProducetime(storageStock.getProducetime());
			management.setNproducetime(nproducetime);
			management.setGoodsbatchs(storageStock.getGoodsbatchs());
			management.setNgoodsbatchs(ngoodsbatchs);
			management.setPutTime(storageStock.getPutTime());
			management.setNputTime(nputTime);
			management.setCreateUser(user.getUserId());
			management.setCreateUserName(user.getName());
			management.setNodes(nodes);
			management.setDeliverId(user.getDeliverId());
			management.setDeptId(user.getDeptId());

    		if(cnumbig.compareTo(num) == 0) {//全部处理
    			management.setStockId(Long.parseLong(storageStockParam.getId().toString()));
    			management.setCnum(storageStock.getNum());
    			management.setCVolume(storageStock.getStockVolume());
    			storageStockManagementService.add(management);//新增处置记录

    			storageStockParam.setProducetime(nproducetime);
    			storageStockParam.setGoodsbatchs(ngoodsbatchs);
    			storageStockParam.setPutTime(nputTime);
    			storageStockService.update(storageStockParam);//更新库存

    			storageStock.setPutTime(nputTime);
    			storageStockDetailedService.addNewDetailedstorageStock("Inventory Disposal", "", "Edit", "Original Inbound Batch:"+management.getPutTime()+",Original Production Date:"+management.getProducetime()+",Original Batch:"+management.getGoodsbatchs(), storageStock, user);//新增库存流水
    		} else {//处理部分
    			BigDecimal sub = num.subtract(cnumbig).setScale(3, BigDecimal.ROUND_HALF_DOWN);
    			BigDecimal p = new BigDecimal(basicGoods.getGoodsCharging());
    			BigDecimal v = new BigDecimal(basicGoods.getGoodsVolume());
    			BigDecimal w = new BigDecimal(basicGoods.getGoodsWeight());
    			//去除被处理的原库存
    			ToolUtil.copyProperties(storageStock, storageStockParam);
    			storageStockParam.setNum(sub.toString());
    			storageStockParam.setCanNum(sub.toString());
    			storageStockParam.setBalenum(sub.toString());
    			storageStockParam.setStockWeight(sub.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
    			storageStockParam.setStockVolume(sub.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
    			storageStockParam.setStockMoney(sub.multiply(p).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
	       		storageStockService.update(storageStockParam);

    			//新增被处理的库存
	            String uuid = UUID.randomUUID().toString().replaceAll("-","");
	            storageStockParam.setId(null);
	            storageStockParam.setMark(uuid);
    			storageStockParam.setNum(cnumbig.toString());
    			storageStockParam.setCanNum(cnumbig.toString());
    			storageStockParam.setBalenum(cnumbig.toString());
    			storageStockParam.setStockWeight(cnumbig.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
    			storageStockParam.setStockVolume(cnumbig.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
    			storageStockParam.setStockMoney(cnumbig.multiply(p).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
    			storageStockParam.setProducetime(nproducetime);
    			storageStockParam.setGoodsbatchs(ngoodsbatchs);
    			storageStockParam.setPutTime(nputTime);
    			storageStockService.add(storageStockParam);

    			StorageStockParam p2 = new StorageStockParam();
    			p2.setMark(uuid);
				StorageStockResult storageStockResult = storageStockService.findBySpec2(p2);

    			management.setStockId(Long.parseLong(storageStockResult.getId().toString()));
    			management.setCnum(storageStockResult.getNum());
    			management.setCVolume(storageStockResult.getStockVolume());
    			storageStockManagementService.add(management);//新增处置记录

    			storageStockDetailedService.addNewDetailedResult("Inventory Disposal", "", "Edit", "Original Inbound Batch:"+management.getPutTime()+",Original Production Date:"+management.getProducetime()+",Original Batch:"+management.getGoodsbatchs(), storageStockResult, user);//新增库存流水
    		}
    	}
        return ResponseData.success();
    }

    /**
     * 存货处置
     * 冻结页面
     */
    @RequestMapping("/dongjie")
    public String dongjie() {
        return PREFIX + "/chuzhi_dongjie.html";
    }

    /**
     * 存货处置
     * 冻结
     */
    @RequestMapping("/chuzhiDongjie")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public ResponseData chuzhiDongjie(StorageStockParam storageStockParam,String cnum,String nodes) {
    	User user = userService.getcurrentUser();
    	StorageStock storageStock = storageStockService.getById(storageStockParam.getId());
    	BasicArea basicArea = basicAreaService.getById(storageStock.getAreaId());
    	BasicGoods basicGoods = basicGoodsService.getById(storageStock.getGoodsId());

		BigDecimal as = new BigDecimal(0);
		BigDecimal num = new BigDecimal(storageStock.getNum());//库存数
		BigDecimal cnumbig = new BigDecimal(cnum);//处理数
		if(cnumbig.compareTo(as) == -1 || cnumbig.compareTo(as) == 0) {
	        return ResponseData.error("Cannot be less than or equal to 0");
		}
		if(cnumbig.compareTo(num) == 1) {
	        return ResponseData.error("Cannot be greater than inventory quantity");
		}

		StorageStockManagementParam management = new StorageStockManagementParam();//处置记录
		management.setPanType("Freeze");
		management.setWarehouseId(storageStock.getWarehouseId());
		management.setWarehouseName(storageStock.getWarehouseName());
		management.setAreaId(storageStock.getAreaId());
		management.setAreaName(basicArea.getAreaName());
		management.setPositionId(storageStock.getPositionId());
		management.setPositionNumber(storageStock.getPositionNumber());
		management.setTrayId(storageStock.getTrayId());
		management.setTrayNumber(storageStock.getTrayNumber());
		management.setGoodsId(storageStock.getGoodsId());
		management.setGoodsName(storageStock.getGoodsName());
		management.setGoodsNumber(storageStock.getGoodsNumber());
		management.setGoodsUnit(basicGoods.getGoodsUnit());
		management.setGoodsSpecs(basicGoods.getGoodsSpecs());
		management.setNum(storageStock.getNum());
		management.setVolume(storageStock.getStockVolume());
		management.setPutTime(storageStock.getPutTime());
		management.setNputTime(storageStock.getPutTime());
		management.setCreateUser(user.getUserId());
		management.setCreateUserName(user.getName());
		management.setNodes(nodes);
		management.setDeliverId(user.getDeliverId());
		management.setDeptId(user.getDeptId());

		if(cnumbig.compareTo(num) == 0) {//全部处理
			management.setStockId(Long.parseLong(storageStockParam.getId().toString()));
			management.setCnum(storageStock.getNum());
			management.setCVolume(storageStock.getStockVolume());
			storageStockManagementService.add(management);//新增处置记录

			storageStockParam.setStockstate("Frozen");
			storageStockService.update(storageStockParam);//更新库存

			storageStockDetailedService.addNewDetailedstorageStock("Inventory Disposal", "", "Freeze", "", storageStock, user);//新增库存流水
		} else {//处理部分
			BigDecimal sub = num.subtract(cnumbig).setScale(3, BigDecimal.ROUND_HALF_DOWN);
			BigDecimal p = new BigDecimal(basicGoods.getGoodsCharging());
			BigDecimal v = new BigDecimal(basicGoods.getGoodsVolume());
			BigDecimal w = new BigDecimal(basicGoods.getGoodsWeight());
			//去除被处理的原库存
			ToolUtil.copyProperties(storageStock, storageStockParam);
			storageStockParam.setNum(sub.toString());
			storageStockParam.setCanNum(sub.toString());
			storageStockParam.setBalenum(sub.toString());
			storageStockParam.setStockWeight(sub.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
			storageStockParam.setStockVolume(sub.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
			storageStockParam.setStockMoney(sub.multiply(p).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
       		storageStockService.update(storageStockParam);

			//新增被处理的库存
            String uuid = UUID.randomUUID().toString().replaceAll("-","");
            storageStockParam.setId(null);
            storageStockParam.setStockstate("Frozen");
            storageStockParam.setMark(uuid);
			storageStockParam.setNum(cnumbig.toString());
			storageStockParam.setCanNum(cnumbig.toString());
			storageStockParam.setBalenum(cnumbig.toString());
			storageStockParam.setStockWeight(cnumbig.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
			storageStockParam.setStockVolume(cnumbig.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
			storageStockParam.setStockMoney(cnumbig.multiply(p).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
			storageStockService.add(storageStockParam);

			StorageStockParam p2 = new StorageStockParam();
			p2.setMark(uuid);
			StorageStockResult storageStockResult = storageStockService.findBySpec2(p2);

			management.setStockId(Long.parseLong(storageStockResult.getId().toString()));
			management.setCnum(storageStockResult.getNum());
			management.setCVolume(storageStockResult.getStockVolume());
			storageStockManagementService.add(management);//新增处置记录

			storageStockDetailedService.addNewDetailedResult("Inventory Disposal", "", "Freeze", "", storageStockResult, user);//新增库存流水
		}
        return ResponseData.success();
    }

    /**
     * 存货处置
     * 解冻页面
     */
    @RequestMapping("/jiedong")
    public String jiedong() {
        return PREFIX + "/chuzhi_jiedong.html";
    }

    /**
     * 存货处置
     * 解冻
     */
    @RequestMapping("/chuzhiJiedong")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public ResponseData chuzhiJiedong(StorageStockParam storageStockParam,String cnum,String nodes) {
    	User user = userService.getcurrentUser();
    	StorageStock storageStock = storageStockService.getById(storageStockParam.getId());
    	BasicArea basicArea = basicAreaService.getById(storageStock.getAreaId());
    	BasicGoods basicGoods = basicGoodsService.getById(storageStock.getGoodsId());

		BigDecimal as = new BigDecimal(0);
		BigDecimal num = new BigDecimal(storageStock.getNum());//库存数
		BigDecimal cnumbig = new BigDecimal(cnum);//处理数
		if(cnumbig.compareTo(as) == -1 || cnumbig.compareTo(as) == 0) {
	        return ResponseData.error("Cannot be less than or equal to 0");
		}
		if(cnumbig.compareTo(num) == 1) {
	        return ResponseData.error("Cannot be greater than inventory quantity");
		}

		StorageStockManagementParam management = new StorageStockManagementParam();//处置记录
		management.setPanType("Thaw");
		management.setWarehouseId(storageStock.getWarehouseId());
		management.setWarehouseName(storageStock.getWarehouseName());
		management.setAreaId(storageStock.getAreaId());
		management.setAreaName(basicArea.getAreaName());
		management.setPositionId(storageStock.getPositionId());
		management.setPositionNumber(storageStock.getPositionNumber());
		management.setTrayId(storageStock.getTrayId());
		management.setTrayNumber(storageStock.getTrayNumber());
		management.setGoodsId(storageStock.getGoodsId());
		management.setGoodsName(storageStock.getGoodsName());
		management.setGoodsNumber(storageStock.getGoodsNumber());
		management.setGoodsUnit(basicGoods.getGoodsUnit());
		management.setGoodsSpecs(basicGoods.getGoodsSpecs());
		management.setNum(storageStock.getNum());
		management.setVolume(storageStock.getStockVolume());
		management.setPutTime(storageStock.getPutTime());
		management.setNputTime(storageStock.getPutTime());
		management.setCreateUser(user.getUserId());
		management.setCreateUserName(user.getName());
		management.setNodes(nodes);
		management.setDeliverId(user.getDeliverId());
		management.setDeptId(user.getDeptId());

		if(cnumbig.compareTo(num) == 0) {//全部处理
			management.setStockId(Long.parseLong(storageStockParam.getId().toString()));
			management.setCnum(storageStock.getNum());
			management.setCVolume(storageStock.getStockVolume());
			storageStockManagementService.add(management);//新增处置记录

			storageStockParam.setStockstate("In Stock");
			storageStockService.update(storageStockParam);//更新库存

			storageStockDetailedService.addNewDetailedstorageStock("Inventory Disposal", "", "Thaw", "", storageStock, user);//新增库存流水
		} else {//处理部分
			BigDecimal sub = num.subtract(cnumbig).setScale(3, BigDecimal.ROUND_HALF_DOWN);
			BigDecimal p = new BigDecimal(basicGoods.getGoodsCharging());
			BigDecimal v = new BigDecimal(basicGoods.getGoodsVolume());
			BigDecimal w = new BigDecimal(basicGoods.getGoodsWeight());
			//去除被处理的原库存
			ToolUtil.copyProperties(storageStock, storageStockParam);
			storageStockParam.setNum(sub.toString());
			storageStockParam.setCanNum(sub.toString());
			storageStockParam.setBalenum(sub.toString());
			storageStockParam.setStockWeight(sub.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
			storageStockParam.setStockVolume(sub.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
			storageStockParam.setStockMoney(sub.multiply(p).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
       		storageStockService.update(storageStockParam);

			//新增被处理的库存
            String uuid = UUID.randomUUID().toString().replaceAll("-","");
            storageStockParam.setId(null);
            storageStockParam.setStockstate("In Stock");
            storageStockParam.setMark(uuid);
			storageStockParam.setNum(cnumbig.toString());
			storageStockParam.setCanNum(cnumbig.toString());
			storageStockParam.setBalenum(cnumbig.toString());
			storageStockParam.setStockWeight(cnumbig.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
			storageStockParam.setStockVolume(cnumbig.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
			storageStockParam.setStockMoney(cnumbig.multiply(p).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
			storageStockService.add(storageStockParam);

			StorageStockParam p2 = new StorageStockParam();
			p2.setMark(uuid);
			StorageStockResult storageStockResult = storageStockService.findBySpec2(p2);

			management.setStockId(Long.parseLong(storageStockResult.getId().toString()));
			management.setCnum(storageStockResult.getNum());
			management.setCVolume(storageStockResult.getStockVolume());
			storageStockManagementService.add(management);//新增处置记录

			storageStockDetailedService.addNewDetailedResult("Inventory Disposal", "", "Thaw", "", storageStockResult, user);//新增库存流水
		}
        return ResponseData.success();
    }

    /**
     * 存货处置
     * 报废页面
     */
    @RequestMapping("/baofei")
    public String baofei() {
        return PREFIX + "/chuzhi_baofei.html";
    }

    /**
     * 存货处置
     * 报废
     */
    @RequestMapping("/chuzhiBaofei")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public ResponseData chuzhiBaofei(StorageStockParam storageStockParam,String cnum,String nodes) {
    	User user = userService.getcurrentUser();
    	StorageStock storageStock = storageStockService.getById(storageStockParam.getId());
    	BasicArea basicArea = basicAreaService.getById(storageStock.getAreaId());
    	BasicGoods basicGoods = basicGoodsService.getById(storageStock.getGoodsId());

		BigDecimal as = new BigDecimal(0);
		BigDecimal num = new BigDecimal(storageStock.getNum());//库存数
		BigDecimal cnumbig = new BigDecimal(cnum);//处理数
		if(cnumbig.compareTo(as) == -1 || cnumbig.compareTo(as) == 0) {
	        return ResponseData.error("Cannot be less than or equal to 0");
		}
		if(cnumbig.compareTo(num) == 1) {
	        return ResponseData.error("Cannot be greater than inventory quantity");
		}

		StorageStockManagementParam management = new StorageStockManagementParam();//处置记录
		management.setPanType("Scrap");
		management.setWarehouseId(storageStock.getWarehouseId());
		management.setWarehouseName(storageStock.getWarehouseName());
		management.setAreaId(storageStock.getAreaId());
		management.setAreaName(basicArea.getAreaName());
		management.setPositionId(storageStock.getPositionId());
		management.setPositionNumber(storageStock.getPositionNumber());
		management.setTrayId(storageStock.getTrayId());
		management.setTrayNumber(storageStock.getTrayNumber());
		management.setGoodsId(storageStock.getGoodsId());
		management.setGoodsName(storageStock.getGoodsName());
		management.setGoodsNumber(storageStock.getGoodsNumber());
		management.setGoodsUnit(basicGoods.getGoodsUnit());
		management.setGoodsSpecs(basicGoods.getGoodsSpecs());
		management.setNum(storageStock.getNum());
		management.setVolume(storageStock.getStockVolume());
		management.setPutTime(storageStock.getPutTime());
		management.setNputTime(storageStock.getPutTime());
		management.setCreateUser(user.getUserId());
		management.setCreateUserName(user.getName());
		management.setNodes(nodes);
		management.setDeliverId(user.getDeliverId());
		management.setDeptId(user.getDeptId());

		if(cnumbig.compareTo(num) == 0) {//全部处理
			management.setStockId(Long.parseLong(storageStockParam.getId().toString()));
			management.setCnum(storageStock.getNum());
			management.setCVolume(storageStock.getStockVolume());
			storageStockManagementService.add(management);//新增处置记录

			storageStockParam.setStockstate("In Stock");
			storageStockService.delete(storageStockParam);//删除库存

			storageStockDetailedService.addNewDetailedstorageStock("Inventory Disposal", "", "Scrap", "", storageStock, user);//新增库存流水
		} else {//处理部分
			BigDecimal sub = num.subtract(cnumbig).setScale(3, BigDecimal.ROUND_HALF_DOWN);
			BigDecimal p = new BigDecimal(basicGoods.getGoodsCharging());
			BigDecimal v = new BigDecimal(basicGoods.getGoodsVolume());
			BigDecimal w = new BigDecimal(basicGoods.getGoodsWeight());
			//去除被处理的原库存
			ToolUtil.copyProperties(storageStock, storageStockParam);
			storageStockParam.setNum(sub.toString());
			storageStockParam.setCanNum(sub.toString());
			storageStockParam.setBalenum(sub.toString());
			storageStockParam.setStockWeight(sub.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
			storageStockParam.setStockVolume(sub.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
			storageStockParam.setStockMoney(sub.multiply(p).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
       		storageStockService.update(storageStockParam);

			storageStockParam.setNum(cnumbig.toString());
			storageStockParam.setCanNum(cnumbig.toString());
			storageStockParam.setBalenum(cnumbig.toString());
			storageStockParam.setStockWeight(cnumbig.multiply(w).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
			storageStockParam.setStockVolume(cnumbig.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
			storageStockParam.setStockMoney(cnumbig.multiply(p).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());

			management.setStockId(Long.parseLong(storageStockParam.getId().toString()));
			management.setCnum(cnumbig.toString());
			management.setCVolume(cnumbig.multiply(v).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());
			storageStockManagementService.add(management);//新增处置记录

			storageStockDetailedService.addNewDetailed("Inventory Disposal", "", "Scrap", "", storageStockParam, user);//新增库存流水
		}
        return ResponseData.success();
    }

    /**
     * 编辑接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(StorageStockParam storageStockParam) {
        storageStockParam.setBalenum(storageStockParam.getNum());
        this.storageStockService.update(storageStockParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(StorageStockParam storageStockParam) {
        this.storageStockService.delete(storageStockParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(StorageStockParam storageStockParam) {
        StorageStock detail = this.storageStockService.getById(storageStockParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(StorageStockParam storageStockParam,String condition ) {
        return this.storageStockService.findPageBySpec(storageStockParam,condition);
    }

    /**
     * 查询库存监控按品列表
     * 在库、冻结
     */
    @ResponseBody
    @RequestMapping("/stockListGoodsChuzhi")
    public LayuiPageInfo stockListGoodsChuzhi(StorageStockParam storageStockParam) {
    	User user = userService.getcurrentUser();
    	storageStockParam.setDeptId(user.getDeptId());
        return this.storageStockService.findStockPageBySpecGoodsChuzhi(storageStockParam,user.getOperateId());
    }

    /**
     * 下载库存模板
     * @throws ServletException , IOException
     */
    @RequestMapping("downloadExcelTemplate")
    public void downloadExcelTemplate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String path = "assets/excel/storageStock.xlsx";
        try {
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(path);
            OutputStream  outputStream = response.getOutputStream();
            //设置下载的文件类型
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            //设置下载文件名
            response.setHeader("Content-Disposition", "attachment;filename=" + new String(("Inventory Import Template" + ".xlsx").getBytes(), "iso-8859-1"));
            IOUtils.copy(inputStream, outputStream);
            outputStream.flush();
            inputStream.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

	@Autowired
    private DeptService deptService;

	/**
	 * 获取所属项目单号前缀
	 * @param user
	 * @return
	 */
	public String getOperateIdOrderPrefix(User user) {
		if(user.getOperateId() != null) {
			Dept dept = new Dept();
			dept.setOperateId(user.getOperateId());
			Dept resdept = deptService.findBySpec(dept);
			if(resdept != null) {
				if(ToolUtil.isNotEmpty(resdept.getOrderPrefix())) {
					return resdept.getOrderPrefix();
				} else {
					return user.getAccount().substring(0, 3);
				}
			} else {
				return null;
			}
		} else {
			return null;
		}
	}

    /**
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(StorageStockParam storageStockParam) {
    	User user = userService.getcurrentUser();
    	if(user.getOperateId() != null) {
            return ResponseData.error("No permission to add inventory");
    	}
        BasicOperate basicOperate = basicOperateService.getById(storageStockParam.getOperateId());
        storageStockParam.setOperateName(basicOperate.getOperateName());//分公司名
        BasicPosition basicPosition = basicPositionService.getById(storageStockParam.getPositionId());
        storageStockParam.setPositionNumber(basicPosition.getPositionNumber());//库位
        //托盘信息
		String trayCode = "add-";
        BasicTrayParam Tp = new BasicTrayParam();
        BasicTray basicTray = basicTrayService.findByThelastone();
        Tp.setTrayState("Occupied");//托盘状态
        int TrayNumber = 0;
        if (basicTray!=null) {
            TrayNumber = (basicTray.getId()+1);
        } else {
            TrayNumber = 1;
        }
		Tp.setDeliverId(basicPosition.getDeliverId());
		Tp.setDeptId(basicPosition.getDeptId());

		BasicTrayParam trayParam = new BasicTrayParam();
		trayParam.setDeliverId(basicPosition.getDeliverId());
		trayParam.setDeptId(basicPosition.getDeptId());
		trayParam.setTrayState("闲置");
		BasicTrayResult basicTrayResult = basicTrayService.findBySpec(trayParam,Long.parseLong(basicOperate.getId().toString()));
		if (basicTrayResult != null) {
			storageStockParam.setTrayNumber(basicTrayResult.getTrayNumber());
			storageStockParam.setTrayId(Long.parseLong(basicTrayResult.getId().toString()));
			basicTrayResult.setTrayState("Occupied");
			ToolUtil.copyProperties(basicTrayResult, trayParam);
			basicTrayService.update(trayParam);
		} else {
			Tp.setTrayNumber(trayCode+TrayNumber);
			basicTrayService.add(Tp);
			trayParam.setTrayNumber(trayCode+TrayNumber);
			trayParam.setTrayState("占用");
			basicTrayResult = basicTrayService.findBySpec(trayParam,Long.parseLong(basicOperate.getId().toString()));
			storageStockParam.setTrayNumber(trayCode+TrayNumber);//托盘编号
			storageStockParam.setTrayId(Long.parseLong(basicTrayResult.getId().toString()));
		}

        BasicWarehouse basicWarehouse = basicWarehouseService.getById(storageStockParam.getWarehouseId());
        storageStockParam.setWarehouseName(basicWarehouse.getWarehouseName());//仓库名

        BasicCustomer basicCustomer = basicCustomerService.getById(storageStockParam.getCustomerId());
        storageStockParam.setCustomerName(basicCustomer.getCustomerName());//客户名称

        BasicArea basicArea  = basicAreaService.getById(storageStockParam.getAreaId());
        storageStockParam.setAreaNumber(basicArea.getAreaNumber());//库区号

        storageStockParam.setBalenum(storageStockParam.getNum());
        storageStockParam.setCanNum(storageStockParam.getNum());

        BasicGoods basicGoods = basicGoodsService.getById(storageStockParam.getGoodsId());
		if (ToolUtil.isNotEmpty(basicGoods.getGoodsWeight())) {
			BigDecimal w = new BigDecimal(basicGoods.getGoodsWeight());
			BigDecimal n = new BigDecimal(storageStockParam.getNum());
			storageStockParam.setStockWeight(w.multiply(n).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//重量
		} else {
			storageStockParam.setStockWeight("1");//重量
		}

		if (ToolUtil.isNotEmpty(basicGoods.getGoodsVolume())) {
			BigDecimal gv = new BigDecimal(basicGoods.getGoodsVolume());
			BigDecimal n = new BigDecimal(storageStockParam.getNum());
			storageStockParam.setStockVolume(gv.multiply(n).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//体积
		} else {
			storageStockParam.setStockVolume("1");//体积
		}

		if (ToolUtil.isNotEmpty(basicGoods.getGoodsCharging())) {
			BigDecimal p = new BigDecimal(basicGoods.getGoodsCharging());
			BigDecimal n = new BigDecimal(storageStockParam.getNum());
			storageStockParam.setStockMoney(p.multiply(n).setScale(3, BigDecimal.ROUND_HALF_DOWN).toString());//库存金额
			storageStockParam.setUnitprice(basicGoods.getGoodsCharging());//单价
		} else {
			storageStockParam.setUnitprice("1");//单价
			storageStockParam.setStockMoney(storageStockParam.getNum());//库存金额
		}

        storageStockParam.setDeliverId(basicPosition.getDeliverId());
        storageStockParam.setDeptId(basicPosition.getDeptId());
        storageStockParam.setStockstate("In Stock");

        if(ToolUtil.isEmpty(storageStockParam.getProducetime())) {
        	storageStockParam.setProducetime(DateUtil.getDate());
        }
        if(ToolUtil.isEmpty(storageStockParam.getGoodsbatchs())) {
        	storageStockParam.setGoodsbatchs(storageStockParam.getProducetime().replaceAll("-", ""));
        }

        if (ToolUtil.isEmpty(storageStockParam.getPutTime())) {
        	storageStockParam.setPutTime(DateUtil.getDate());
		}
        this.storageStockService.add(storageStockParam);
        return ResponseData.success();
    }

    /**
     * 新上传
     * @param file
     * @param storageStockParam
     * @return
     */
    @RequestMapping(value = "/import" , method = {RequestMethod.POST} )
    @ResponseBody
    public ResponseData addImport(@RequestPart("file") MultipartFile file,StorageStockParam storageStockParam) {
        ResponseData data = new ResponseData();
        String fileName = file.getOriginalFilename();
        if (fileName != null){
            try {
               data = storageStockService.batchImport(fileName, file,storageStockParam,data);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return data;
    }

    /**
     * 跳转到 贝因美库房图
     */
    @RequestMapping("kft_bym")
    public String kft_bym(Model model) {
        return PREFIX + "/kft_bym.html";
    }

    /**
     * 跳转到 青岛库房图
     */
    @RequestMapping("kft_qd")
    public String kft_qd(Model model) {
        return PREFIX + "/kft_qd.html";
    }

    /**
     * 跳转到 华熙库房图
     */
    @RequestMapping("kft_hx")
    public String kft_hx(Model model) {
        return PREFIX + "/kft_hx.html";
    }

    /**
     * 跳转到 软风库房图
     */
    @RequestMapping("kft_rf")
    public String kft_rf(Model model) {
        return PREFIX + "/kft_rf.html";
    }

    /**
     * 跳转到 华熙材料库房图
     */
    @RequestMapping("kft_hxclk")
    public String kft_hxclk(Model model) {
        return PREFIX + "/kft_hxclk.html";
    }

    /**
     * 查询 库房图数据
     * @return
     */
    @ResponseBody
    @RequestMapping("/findkft_bym")
    public List<Kfsum> findkft_bym(BasicPositionParam basicPositionParam){
    	List<Kfsum> list = basicPositionService.findkfsum(basicPositionParam);//查询库位存货情况

		StorageStockParam storageStockParam = new StorageStockParam();
		storageStockParam.setWarehouseId(Long.parseLong(basicPositionParam.getWarehouseId().toString()));
		List<Kfgoods> list2 =  storageStockService.findkfgoods(storageStockParam);
    	for (Kfsum k : list) {
    		Integer MKS = 0;
    		Integer YFS = 0;
			if(ToolUtil.isNotEmpty(k.getMKS())) {
				MKS = Integer.parseInt(k.getMKS());
			}
			if(ToolUtil.isNotEmpty(k.getYFS())) {
				YFS = Integer.parseInt(k.getYFS());
			}
			if(MKS == YFS && YFS > 0) {
				k.setZT("Full");
			}  else if (YFS > MKS && YFS > 0) {
				k.setZT("Full");
			} else if (YFS < MKS && YFS > 0) {
				k.setZT("Partial");
			} else {
				k.setZT("Empty");
				continue;
			}
			List<Kfgoods> list3 = new ArrayList<Kfgoods>();
			for (Kfgoods s : list2) {
				if(k.getKwid().longValue() == s.getPositionId().longValue()) {
					list3.add(s);
				}
			}
			k.setList(list3);
		}
        return list;
    }

    /**
     * 批量删除接口
     */
    @RequestMapping("/btnRevoke")
    @ResponseBody
    public ResponseData btnRevoke(Integer[] ids) {
    	User user = userService.getcurrentUser();
    	if(user.getOperateId() != null) {
            return ResponseData.error("No permission to delete inventory");
    	}
    	for (Integer id : ids) {
    		StorageStockParam storageStockParam = new StorageStockParam();
    		storageStockParam.setId(id);
			this.storageStockService.delete(storageStockParam);
		}
        return ResponseData.success();
    }

    @RequestMapping("taskJob")
    public void taskJob() {
    	storageStockService.taskJob();
    }

    /**
     * 库存监控选择库位页面
     */
    @RequestMapping("/toPositionStock")
    public String toPositionStock() {
        return PREFIX + "/positionStock.html";
    }

}


